package com.leruan.aasimp.action.cosmetology;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;

import com.leruan.aasimp.constant.UsedCarConstant;
import com.leruan.aasimp.entity.CarInfo;
import com.leruan.aasimp.entity.CarInsuranceCom;
import com.leruan.aasimp.entity.CarMainInfo;
import com.leruan.aasimp.entity.CarMrOdd;
import com.leruan.aasimp.entity.Eauser;
import com.leruan.aasimp.entity.MtInfoEntity;
import com.leruan.aasimp.entity.UserTypeEnumEntity;
import com.leruan.aasimp.service.CosmetologyService;
import com.leruan.aasimp.util.DateUtils;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 汽车美容
 * @author 沈王冰
 */
public class CosmetologyAction extends ActionSupport {

	private static final long serialVersionUID = 4603945367394812894L;
	
	private CosmetologyService cosmetologyService; 
	//首页
	public String showMain(){
		HttpServletRequest request = ServletActionContext.getRequest();

		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		if(eauser!=null&&eauser.getIdCompany()!=null){
			CarInsuranceCom company = cosmetologyService.getCompanyByUid(eauser.getIdCompany());
			request.getSession().setAttribute("companyInfo", company);
		}
		
		HttpSession session = request.getSession();
		int pageNo = 1;
		if(request.getParameter("no")!=null){
			pageNo = Integer.parseInt(request.getParameter("no"));
		}
		List<CarInfo> infos = cosmetologyService.getAllMrInfo(pageNo);
		List<MtInfoEntity> mtinfo = new ArrayList<MtInfoEntity>();
		for(CarInfo o : infos){
			Eauser eauser2 = cosmetologyService.getEauserByUid(o.getUserid());
			System.out.println(eauser2.getUserId());
			CarInsuranceCom company = cosmetologyService.getCompanyByUid(eauser2.getIdCompany());
			MtInfoEntity mt = new MtInfoEntity();
			mt.setId(o.getId()+"");
			mt.setTitle(o.getTitle());
			mt.setName(o.getName());
			mt.setMobile(o.getMobile()+"");
			if(company!=null){
				mt.setCompanyName(company.getCompanyname());
			}
			mt.setReleaseTime(o.getReleaseTime());
			mt.setPrice(o.getPrice());
			mtinfo.add(mt);
		}
		Long total = cosmetologyService.getMrCount();
		session.setAttribute("mrInfos", mtinfo);
		session.setAttribute("mrpageNoSale", pageNo);
		session.setAttribute("mrpageNoCenterSale", 3);
		session.setAttribute("mrtotalNoSale", total / UsedCarConstant.PAGE_SIZE + 1);
		session.setAttribute("mrtotalSale", total);
		
		return "main";
	}
	//美容申请
	public String showApply(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		
		List<CarMainInfo> carMain = cosmetologyService.getCarManagesByUc(eauser.getUsercard());
		if(carMain==null||carMain.size()==0){
			request.setAttribute("message", "对不起，您没有任何车辆！");
			return showMain();
		}
		
		JSONArray car = JSONArray.fromObject(carMain);
		request.setAttribute("carMain", car.toString().replace("\"", "'"));
		
		List<CarMrOdd> mrodds = cosmetologyService.getMrOddsByUn(eauser.getUsername());
//		for(CarMrOdd o : mrodds){
//			CarCompanyInfo company = cosmetologyService.getCompanyByUid(o.getOddCompany());
//			o.setOddCompany(company.getCompanyName());
//		}
		Collections.sort(mrodds, new Comparator<CarMrOdd>() {
			public int compare(CarMrOdd arg0,
					CarMrOdd arg1) {
				return ((Timestamp) arg1.getOddDate())
						.compareTo((Timestamp) arg0.getOddDate());
			}
		});
		
		JSONArray array = JSONArray.fromObject(mrodds);
		System.out.println(array.toString()); 
		request.setAttribute("MrOdds", array.toString().replace("\"", "'"));
		choiceApplyMr();
		return "apply";
	}
	//增加申请单or修改
	public String addApply(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		Long id = Long.parseLong(request.getParameter("oddId"));
		String cd = request.getParameter("createDate");
		Date createDate = DateUtils.string2Date(cd);
		String bt = request.getParameter("oddDate");
		Date oddDate = DateUtils.string2Date(bt);
		String cn = request.getParameter("oddCompany");
		String tel = request.getParameter("persontel");
		String email = request.getParameter("personemail");
		String vin = request.getParameter("vin");
		String message = request.getParameter("remark");
		CarMrOdd mrOdd = new CarMrOdd(id,eauser.getUsername(), tel,
				email, oddDate, createDate,
			 cn, vin, "1", message);
		String upid = request.getParameter("id");
		if(upid!=null){
			CarMrOdd odd = cosmetologyService.getMrOddsById(id);
			mrOdd.setId(odd.getId());
			cosmetologyService.updateApply(mrOdd);
		}else{
			cosmetologyService.addApply(mrOdd);
		}
		return showApply();
	}
	//删除申请单
	public String destroyApply(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Long id = Long.parseLong(request.getParameter("id"));
		CarMrOdd odd = cosmetologyService.getMrOddsById(id);
		odd.setStatus("0");
		cosmetologyService.updateApply(odd);
		return showApply();
	}
	//选择美容公司
	public String choiceApplyMr(){
		HttpServletRequest request = ServletActionContext.getRequest();
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		List<Eauser> eausers = cosmetologyService.getEausersByType(UserTypeEnumEntity.CARBEAUTY.getValue());
		for(Eauser o : eausers){
			Map<String,Object> map = new HashMap<String, Object>();
			CarInsuranceCom company = cosmetologyService.getCompanyByUid(o.getIdCompany());
			if(company.getStatus().equals("0")){
				continue;
			}
			
			map.put("id", o.getUserId());
			map.put("name", company.getCompanyname());
			map.put("address", company.getCompanyAdd());
			map.put("username", o.getName());
			map.put("tel", o.getTel());
			list.add(map);
		}
		
		JSONArray array = JSONArray.fromObject(list);
		
		System.out.println(array.toString()); 
		
		request.setAttribute("Mrcompany", array.toString().replace("\"", "'"));
		return "Mrcompany";
	}
	
	//美容审批
	public String showApprove(){
		return "approve";
	}
	//美容记录
	public String showRecord(){
		return "record";
	}
	//数据挖掘
	public String showBb(){
		return "baob";
	}
	public CosmetologyService getCosmetologyService() {
		return cosmetologyService;
	}
	public void setCosmetologyService(CosmetologyService cosmetologyService) {
		this.cosmetologyService = cosmetologyService;
	}
	
}
